package prepare.huawei;

import java.util.*;

/**
 * description: <a href="https://blog.csdn.net/weixin_44052055/article/details/125833252">区间交集</a>
 *
 * @author <a href="zhaoyingling12@163.com">simon zhao</a>
 * @since 2023/3/24
 */
public class IntervalIntersection {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String[] ss = input.nextLine().split(" ");
        int[][] intervals = new int[ss.length / 2][2];
        for (int i = 0; i < ss.length; i++) {
            intervals[i / 2][0] = Integer.parseInt(ss[i]);
            intervals[i / 2][1] = Integer.parseInt(ss[i + 1]);
            i++;
        }
        // 先排序
        Comparator comparator = new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return Integer.compare(o1[0], o2[0]);
            }
        };
        Arrays.sort(intervals, comparator);
        // step1 先求任意两个区间 交集
        List<int[]> commonInterval = new ArrayList<>();
        for (int i = 0; i < intervals.length - 1; i++) {
            int end = intervals[i][1];
            for (int j = i + 1; j < intervals.length; j++) {
                if (intervals[j][0] > end) {
                    continue;
                }
                if (intervals[j][1] >= end) {
                    commonInterval.add(new int[]{intervals[j][0], end});
                } else {
                    commonInterval.add(new int[]{intervals[j][0], intervals[j][1]});
                }
            }
        }
        // step2 求交集区间的并集
        int start = commonInterval.get(0)[0], end = commonInterval.get(0)[1];
        List<int[]> resList = new ArrayList<>();
        for (int i = 1; i < commonInterval.size(); i++) {
            // [1, 5] [2, 4] [2, 4] 肯定不能扩大区间
            if (commonInterval.get(i)[1] <= end) {
                continue;
            }
            if (commonInterval.get(i)[0] <= end ) {
                // 扩大区间
                end = commonInterval.get(i)[1];
            } else {
                resList.add(new int[]{start, end});
                start = commonInterval.get(i)[0]
                ;
                end = commonInterval.get(i)[1];
            }
        }
        resList.add(new int[]{start, end});
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < resList.size(); i++) {
            if (i != 0) {
                builder.append(" ");
            }
            builder.append(resList.get(i)[0])
                    .append(" ")
                    .append(resList.get(i)[1]);
        }
        System.out.println(builder);
    }
}
